package com.aimosheng.beauty.views;

import android.content.Context;
import androidx.viewpager.widget.ViewPager;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;

import com.aimosheng.beauty.R;
import com.aimosheng.beauty.interfaces.IBeautyClickListener;
import com.aimosheng.beauty.interfaces.IBeautyViewHolder;
import com.aimosheng.common.adapter.ViewPagerAdapter;
import com.aimosheng.common.views.AbsViewHolder;

import java.util.ArrayList;
import java.util.List;

public class BeautyViewHolder extends AbsViewHolder implements IBeautyViewHolder, IBeautyClickListener {

    private ViewPager mViewPager;
    private List<FrameLayout> mViewList;
    private AbsMhChildViewHolder[] mViewHolders;
    private boolean mShowed;
    private VisibleListener mVisibleListener;

    public BeautyViewHolder(Context context, ViewGroup parentView) {
        super(context, parentView);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_beauty;
    }

    @Override
    public void init() {
        mViewPager = (ViewPager) mContentView;
        int pageCount = 5;
        mViewList = new ArrayList<>();
        for (int i = 0; i < pageCount; i++) {
            FrameLayout frameLayout = new FrameLayout(mContext);
            frameLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            mViewList.add(frameLayout);
        }
        if (pageCount > 1) {
            mViewPager.setOffscreenPageLimit(pageCount - 1);
        }
        mViewPager.setAdapter(new ViewPagerAdapter(mViewList));
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                loadPageData(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mViewHolders = new AbsMhChildViewHolder[pageCount];
        loadPageData(0);
    }


    private void loadPageData(int position) {
        if (mViewHolders == null) {
            return;
        }
        AbsMhChildViewHolder vh = mViewHolders[position];
        if (vh == null) {
            if (mViewList != null && position < mViewList.size()) {
                FrameLayout parent = mViewList.get(position);
                if (parent == null) {
                    return;
                }
                if (position == 0) {
                    vh = new MhMainViewHolder(mContext, parent);
                } else if (position == 1) {
                    vh = new MhTieZhiViewHolder(mContext, parent);
                } else if (position == 2) {
                    vh = new MhMeiYanViewHolder(mContext, parent);
                } else if (position == 3) {
                    vh = new MhTeXiaoViewHolder(mContext, parent);
                } else if (position == 4) {
                    vh = new MhHaHaViewHolder(mContext, parent);
                }
                if (vh == null) {
                    return;
                }
                mViewHolders[position] = vh;
                vh.addToParent();
                vh.subscribeActivityLifeCycle();
                vh.setIBeautyClickListener(this);
            }
        }
        if (vh != null) {
            vh.loadData();
        }
    }

    @Override
    public void show() {
        if (mVisibleListener != null) {
            mVisibleListener.onVisibleChanged(true);
        }
        if (mParentView != null && mContentView != null) {
            ViewParent parent = mContentView.getParent();
            if (parent != null) {
                ((ViewGroup) parent).removeView(mContentView);
            }
            mParentView.addView(mContentView);
        }
        mShowed = true;
    }

    @Override
    public void hide() {
        if (mViewPager != null && mViewPager.getCurrentItem() != 0) {
            tabMain();
            return;
        }
        removeFromParent();
        if (mVisibleListener != null) {
            mVisibleListener.onVisibleChanged(false);
        }
        mShowed = false;
    }

    @Override
    public boolean isShowed() {
        return mShowed;
    }

    @Override
    public void setVisibleListener(VisibleListener visibleListener) {
        mVisibleListener = visibleListener;
    }

    @Override
    public void hideView() {
        hide();
    }

    @Override
    public void tabMain() {
        if (mViewPager != null) {
            mViewPager.setCurrentItem(0, false);
        }
    }

    @Override
    public void tabTieZhi() {
        if (mViewPager != null) {
            mViewPager.setCurrentItem(1, false);
        }
    }

    @Override
    public void tabMeiYan() {
        if (mViewPager != null) {
            mViewPager.setCurrentItem(2, false);
        }
    }

    @Override
    public void tabTeXiao() {
        if (mViewPager != null) {
            mViewPager.setCurrentItem(3, false);
        }
    }

    @Override
    public void tabHaHa() {
        if (mViewPager != null) {
            mViewPager.setCurrentItem(4, false);
        }
    }
}
